<?php
/**
 * Created by PhpStorm.
 * User: 554665488
 * Date: 2018-6-17
 * Time: 0:57
 * @description:
 * @Author: yfl
 * @QQ 554665488
 */

namespace App\Http\Controllers\Api\V1;

use Illuminate\Http\Request;
use Illuminate\Contracts\Routing\ResponseFactory as ResponseContract;
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
use Omnipay\Omnipay;
use DB;

class PayController extends ApiBase
{
    public function index()
    {
        echo '帅不帅你说了算';
    }

    public function create(Request $request, ApplicationContract $application, ResponseContract $responseFactory)
    {
        $type = $request->input('type');
        if (!in_array($type, ['AlipayOrder', 'AlipayWapOrder', 'WechatOrder', 'WechatWapOrder'])) {
            return $responseFactory->json(['msg' => '请求支付方式错误'], 422);
        }

        return $application->call([$this, 'get' . $type]);
    }

    public function getAlipayOrder(Request $request, ResponseContract $responseFactory, NativePayOrder $order)
    {
        $user = $request->user();
        $amount = $request->input('amount', 0);
        $from = $request->input('from');
        $config = config('newPay.alipay');
        if (!$amount) {
            return $responseFactory->json(['message' => '提交的信息不完整'], 422);
        }

        $gateWay = Omnipay::create('Alipay_AopApp');
        $gateWay->setSignType($config['signType']);
        $gateWay->setAppId($config['appId']);
        $gateWay->setPrivateKey($config['secretKey']);
        $gateWay->setPrivateKey($config['secretKey']);
        $gateWay->setAlipayPublicKey($config['publicKey']);
        $gateWay->setNotifyUrl(config('app.url', '/ap2/v2/alipay/notify'));
        $order->out_trade_no = date('YmdHis') . mt_rand(1000, 9999) . config('newPay.sign');
        $order->subject = '钱包充值';
        $order->content = '在' . config('app.name') . '充值余额' . $amount / 100 . '元';
        $order->amount = $amount;
        $order->product_code = 'FAST_INSTANT_TRADE_PAY';
        $order->user_id = $user->id;
        $order->from = $from;
        $order->type = 'alipay';
        $walletCharge = $this->createChargeModel($request, 'Alipay-Native');
        $walletOrder = $this->createOrderModel($user->id, intval($amount), 'Native-Alipay', $order->subject);
        $result = $gateWay->purchase()->setBizContent([
            'subject' => $order->subject,
            'out_trade_no' => $order->out_trade_no,
            'total_amount' => $order->amount / 100,
            'product_code' => $order->product_code,
            'body' => $order->content,
            'timeout_express' => '10m',
            'charge_type' => 'wallet',
        ])->send();
        if ($result->isSuccessful()) {
            return DB::transaction(function () use ($order, $walletCharge, $responseFactory, $result, $walletOrder) {
                try {
                    $order->save();
                    $walletOrder->target_id = $order->id;
                    $walletCharge->charge_id = $order->id;
                    $walletCharge->save();
                    $walletOrder->save();
                    return $responseFactory->json(['message' => '订单创建成功', 'data' => $result->getOrderString()], 201);
                } catch (\Exception $e) {
                    Db::rollback();
                    return $responseFactory->json(['message' => '创建支付宝订单失败'], 422);
                }
            });
        }
    }

    public function checkAlipayOrder()
    {
        return '666111';
    }
}